package com.zkjl.protect_business.controller;

import com.zkjl.protect_business.common.ApiResponse;
import com.zkjl.protect_business.common.SystemLog;
import com.zkjl.protect_business.dto.document.UploadAttachmentDTO;
import com.zkjl.protect_business.dto.document.UploadDocumentDTO;
import com.zkjl.protect_business.service.DocumentService;
import com.zkjl.protect_business.utils.ResultUtil;
import com.zkjl.protect_business.vo.PageVO;
import com.zkjl.protect_business.vo.UploadAttachmentVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageImpl;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/business")
@Api(tags = {"文档"})
public class DocumentController {

    @Autowired
    DocumentService documentService;

    @ApiOperation(value = "上传文档", notes = "上传文档", httpMethod = "POST")
    @PostMapping(value = "templates/{templateId}/documents")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "templateId", value = "模板id", required = true, paramType = "path"),
            @ApiImplicitParam(name = "orgId", value = "文档所属机构 可以不传", required = true, paramType = "query")
    })
    @SystemLog(description = "上传文档")
    public ApiResponse save(MultipartFile file, @PathVariable String templateId, UploadDocumentDTO uploadDocumentDTO) throws IOException {

        Object document = documentService.saveDocument(file, templateId, uploadDocumentDTO);
        return ApiResponse.ofSuccess(document);

    }


    @ApiOperation(value = "新增或者修改文档", notes = "新增或者修改文档", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "businessId", value = "业务id", required = true, paramType = "path"),
            @ApiImplicitParam(name = "param", value = "新增就不带id，修改就带上id,修改的时候带上现在所有的数据", required = true, paramType = "query")
    })
    @PostMapping(value = "/{businessId}/documents")
    @SystemLog(description = "新增或者修改文档")
    public ApiResponse saveOrupdate(@RequestBody Map<String, Object> param, @PathVariable String businessId) {
        Object saveOrupdate = documentService.saveOrupdate(businessId, param);
        return ApiResponse.ofSuccess(saveOrupdate);
    }


    @ApiOperation(value = "恢复删除的业务文档", notes = "恢复删除的业务文档", httpMethod = "PUT")
    @PutMapping(value = "recovery/{indexName}")
    public ApiResponse recovery(@PathVariable String indexName) {
        Boolean recovery = documentService.recovery(indexName);
        return ApiResponse.ofSuccess(recovery);
    }


    @ApiOperation(value = "上传附件", notes = "上传附件", httpMethod = "POST")
    @PostMapping(value = "{businessId}/attachments")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "businessId", value = "业务id", required = true, paramType = "path"),
            @ApiImplicitParam(name = "files", value = "附件 附件的文件名中必须包含字段名称和唯一标识,以 - 区分后面的字段名称和唯一标识，字段名中用 = 分隔， 例如 xxxx-身份证号=2121314141.pdf", required = true, paramType = "query"),
            @ApiImplicitParam(name = "filed", value = "附件归属字段", required = true, paramType = "query")
    })
    @SystemLog(description = "上传附件")
    public ApiResponse<UploadAttachmentVO> uploadAttachment(MultipartFile[] files, @PathVariable String businessId, String filed) {
            documentService.uploadAttachment(files, businessId, filed);
        return ApiResponse.ofSuccess();
    }

    @ApiOperation(value = "文档列表", notes = "文档列表  archiveId 存档id 传了就会查询存档里面的数据", httpMethod = "GET")
    @GetMapping(value = "{businessId}/documents")
    @SystemLog(description = "查看文档列表")
    public ApiResponse<PageVO<Object>> page(@PathVariable String businessId, @RequestParam Map<String, Object> param) {

        PageImpl<Object> page = documentService.page(businessId, param);

        ApiResponse apiResponse = ResultUtil.successPagesVO(page);
        return apiResponse;
    }


    @ApiOperation(value = "导出文档", notes = "导出文档", httpMethod = "POST")
    @PostMapping(value = "templates/{templateId}/export")
    @SystemLog(description = "导出文档")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "param", value = "map类型参数 docIds 是根据id导出，根据条件search 标识模糊搜索，其他是自定义字段")
    })
    public ApiResponse export (@RequestBody Map<String, Object> param, @PathVariable String templateId) throws IOException {

        String export = documentService.export(templateId, param);
        return ApiResponse.ofSuccess(export);
    }

    @ApiOperation(value = "删除文档", notes = "删除文档", httpMethod = "DELETE")
    @SystemLog(description = "删除文档")
    @DeleteMapping(value = "{businessId}/documents")
    public ApiResponse del(@PathVariable String businessId, @RequestBody List<String> documentIds) {

        String del = documentService.del(businessId, documentIds);
        return ApiResponse.ofSuccess(del);
    }


}
